package concurrent;

import junit.framework.TestCase;
import org.junit.Test;

import java.util.concurrent.Semaphore;

/**
 * @author zhangxin
 * @date 2020/9/28
 */
public class SemaphoreTest extends TestCase {

    @Test
    public void test() throws InterruptedException {
        Semaphore semaphore = new Semaphore(0);
        semaphore.release();
        semaphore.release();
        semaphore.release();
        semaphore.acquire();
        semaphore.acquire();
        semaphore.acquire();
        semaphore.acquire();

        System.out.println("acquire");

    }

    public static void main(String[] args) {

        Semaphore semaphore = new Semaphore(2);

        new Thread(() -> {
            try {
                semaphore.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t1");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            semaphore.release(10);
        }).start();


        new Thread(() -> {
            try {
                semaphore.acquire(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t2");
            semaphore.release(10);
        }).start();

    }
}
